package com.imti.mall.order.controller;

import com.github.pagehelper.PageHelper;
import com.imti.mall.common.Api.CommonPage;
import com.imti.mall.common.Api.CommonResult;
import com.imti.mall.model.oms.pojo.OmsCompanyAddress;
import com.imti.mall.model.oms.pojo.OmsOrderReturnApply;
import com.imti.mall.model.oms.query.OmsOrderListParam;
import com.imti.mall.order.service.OmsCompanyAddrService;
import com.imti.mall.order.service.OrderReturnApplyService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags="订单退回")
@RequestMapping("api/v1/orders")
public class OrderReturnApplyController {
    @Resource
    private OrderReturnApplyService orderReturnApplyService;
    @Resource
    private OmsCompanyAddrService companyAddrService;
    @GetMapping("/returnApply")
    public CommonResult getApplyList(OmsOrderListParam listParam,OmsOrderReturnApply apply){

        listParam.setPageIndex();
        Map<String,Object> map=new HashMap<>();
        map.put("listParam",listParam);

        map.put("apply",apply);
        List<OmsOrderReturnApply> applyList=orderReturnApplyService.getApplyList(map);
        for(int i=0;i<applyList.size();i++){
            OmsOrderReturnApply returnApply=applyList.get(i);
            if(returnApply.getHandleTime()==null||"".equals(returnApply.getHandleTime())){

                returnApply.setHandleTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
                applyList.set(i,returnApply);
            }
        }
        return CommonResult.success(applyList);
    }

    @GetMapping("/returnApply/{id}")
    public CommonResult getApplyDetail(@PathVariable Integer id){
        OmsOrderReturnApply orderReturnApply=orderReturnApplyService.getApplyDetail(id);
        return CommonResult.success(orderReturnApply);
    }

    @PostMapping("/returnApply/update/status/{id}")
    public  CommonResult   updateApplyStatus(@PathVariable Long id,@RequestBody OmsOrderReturnApply omsOrderReturnApply){
        omsOrderReturnApply.setId(id);

       int n=orderReturnApplyService.updateApplyStatus(omsOrderReturnApply);
        return CommonResult.success(n>0);
    }
    @PostMapping("/returnApply/delete")
    public  CommonResult   deleteReturnApply(@RequestBody Integer[] ids){

        int n=orderReturnApplyService.deleteReturnApply(ids);
        return CommonResult.success(n==ids.length);
    }


    @GetMapping("/companyAddress/list")
    public CommonResult getCompanyAddress(){
           List<OmsCompanyAddress> addressList= companyAddrService.getCompanyAddress();
        return CommonResult.success(addressList);
    }
}
